package com.chinasofti.project.service.user.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinasofti.project.dal.mapper.BzUserMapper;
import com.chinasofti.project.dal.mapper.custom.BzShoppingCartMapperCustom;
import com.chinasofti.project.dal.model.BzUser;
import com.chinasofti.project.dal.model.BzUserExample;
import com.chinasofti.project.dal.model.BzUserExample.Criteria;
import com.chinasofti.project.service.user.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private BzUserMapper bzUserMapper;

	@Autowired
	private BzShoppingCartMapperCustom bzShoppingCartMapperCustom;

	@Override
	public int insertFromRegister(BzUser bzUser) {
		bzUser.setUserLastLogintime(new Date());
		bzUser.setUserNowLogintime(new Date());
		int row = bzUserMapper.insertSelective(bzUser);
		return row;
	}

	@Override
	public int updatePassword(BzUser bzUser) {
		BzUserExample bzUserExample = new BzUserExample();
		String userEmail = bzUser.getUserEmail();
		bzUserExample.getOrderByClause();
		Criteria criteria = bzUserExample.createCriteria();
		criteria.andUserEmailEqualTo(userEmail);
		criteria.andUserEmailIsNotNull();
		int row = bzUserMapper.updateByExampleSelective(bzUser, bzUserExample);
		return row;
	}

	@Override
	public int updateLoginTime(Date nowLogintime, int userId) {
		BzUser bzUser = new BzUser();
		bzUser.setUserId(userId);
		bzUser.setUserLastLogintime(nowLogintime);
		bzUser.setUserNowLogintime(new Date());
		int row = bzUserMapper.updateByPrimaryKeySelective(bzUser);
		return row;
	}

	@Override
	public BzUser SelectUserEmailAndPassword(BzUser bzUser) {
		BzUserExample bzUserExample = new BzUserExample();
		String password = bzUser.getUserPassword();
		String userEmail = bzUser.getUserEmail();
		bzUserExample.getOrderByClause();
		Criteria criteria = bzUserExample.createCriteria();
		criteria.andUserEmailEqualTo(userEmail);
		criteria.andUserEmailIsNotNull();
		criteria.andUserPasswordEqualTo(password);
		criteria.andUserPasswordIsNotNull();
		List<BzUser> listBzUser = bzUserMapper.selectByExample(bzUserExample);
		if (listBzUser != null && listBzUser.size() > 0) {
			return listBzUser.get(0);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.user.UserService#querUserEmail(java.lang.
	 * String)
	 */
	@Override
	public boolean queryUserEmail(String userEmail) {
		BzUserExample bzUserExample = new BzUserExample();
		bzUserExample.getOrderByClause();
		Criteria criteria = bzUserExample.createCriteria();
		criteria.andUserEmailEqualTo(userEmail);
		criteria.andUserEmailIsNotNull();
		List<BzUser> listBzUser = bzUserMapper.selectByExample(bzUserExample);
		if (listBzUser != null && listBzUser.size() > 0) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.user.UserService#updateUserBalance(com.
	 * chinasofti.project.dal.model.BzUser)
	 */
	@Override
	public void updateUserBalance(BzUser bzUser) {
		bzUserMapper.updateByPrimaryKeySelective(bzUser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.chinasofti.project.service.user.UserService#selectNumByUserId(int)
	 */
	@Override
	public int selectNumByUserId(int userId) {
		int shopNum = bzShoppingCartMapperCustom.selectNumByUserId(userId);
		return shopNum;
	}

}
